﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using cpe.cpein.DATA;

namespace cpe.cpein.BUSINESS
{
    public static class BURelationType
    {
        /// <summary>
        /// Get All the RelationType of the database.
        /// </summary>
        /// <param name="searchInDeleted"></param>
        /// <returns></returns>
        public static List<RelationType> GetAllRelationType(bool searchInDeleted)
        {
            List<RelationType> RelationTypes = new List<RelationType>();
            using (cpeinEntities db = new cpeinEntities())
            {
                var results = from RelationType in db.RelationType
                              where RelationType.rlt_isdeleted == searchInDeleted
                              select RelationType;
                foreach (RelationType tmp in results)
                    RelationTypes.Add(tmp);
            }

            return RelationTypes;
        }

        /// <summary>
        /// Get the RelationType by id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static RelationType GetRelationTypeByID(int id)
        {
            if (id <= 0)
                return null;
            else
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var rlt = from RelationType in db.RelationType
                              where RelationType.rlt_id == id
                              select RelationType;
                    foreach (RelationType tmp in rlt)
                        return tmp;
                    return null;
                }
            }
        }

        /// <summary>
        /// rlt a RelationType into the database.
        /// </summary>
        /// <param name="rlt"></param>
        /// <returns></returns>
        public static RelationType RelationTyperea(RelationType rlt)
        {
            if (rlt != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    RelationType nrlt = db.RelationType.Add(rlt);
                    db.SaveChanges();
                    return nrlt;
                }
            }
            else
                return null;
        }

        /// <summary>
        /// Remove a specific RelationType of the database.
        /// </summary>
        /// <param name="rlt"></param>
        /// <returns></returns>
        public static bool RemoveRelationType(RelationType rlt)
        {
            if (rlt != null)
            {
                rlt.rlt_isdeleted = true;
                if (UpdateRelationType(rlt) != null)
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {
                        db.SaveChanges();
                        return true;
                    }
                }
            }

            return false;
        }

        public static bool ChangeRelationType(User user1, User user2, int type)
        {
            using (cpeinEntities db = new cpeinEntities())
            {
                var current = from UserToRelation in db.UserToRelation
                              where (
                                (UserToRelation.usr_id1 == user1.usr_id && UserToRelation.usr_id2 == user2.usr_id)
                                ||
                                (UserToRelation.usr_id1 == user2.usr_id && UserToRelation.usr_id2 == user1.usr_id)
                              )
                              select UserToRelation;

                foreach (var tmp in current)
                {
                    tmp.rel_type = type;
                }

                int num = db.SaveChanges();
                return (num > 0);
            }
        }


        /// <summary>
        /// Update the RelationType into the database.
        /// </summary>
        /// <param name="rlt"></param>
        /// <returns></returns>
        public static RelationType UpdateRelationType(RelationType rlt)
        {
            if (rlt != null)
            {
                using (cpeinEntities db = new cpeinEntities())
                {
                    var current = from RelationType in db.RelationType
                                  where RelationType.rlt_id == rlt.rlt_id
                                  select RelationType;

                    foreach (var tmp in current)
                    {
                        tmp.rlt_value = rlt.rlt_value;
                        tmp.rlt_isdeleted = rlt.rlt_isdeleted;

                        db.SaveChanges();
                    }

                    rlt = current as RelationType;
                }
            }

            return rlt;
        }


        public static RelationType GetRelationType(User user1, User user2)
        {
            {
                if (user1 == null || user2 == null)
                    return null;
                else
                {
                    using (cpeinEntities db = new cpeinEntities())
                    {

                        // Get all current user's relations
                        var relationTypes = ((from usrRel in db.UserToRelation
                                             join rel in db.RelationType on usrRel.rel_type equals rel.rlt_id
                                             where (
                                                (usrRel.usr_id1 == user1.usr_id && usrRel.usr_id2 == user2.usr_id)
                                                ||
                                                (usrRel.usr_id1 == user2.usr_id && usrRel.usr_id2 == user1.usr_id)
                                            )
                                             select rel));
                        RelationType relType = null;
                        try { relType = relationTypes.ToList().First(); }
                        catch (Exception excep) { }

                        return relType;
                    }
                }
            }
        }

    }
}
